<?php

namespace App\Policies;

use App\Models\User;
use App\Models\Quiz;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\Repositories\QuizRepository;

class QuizPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * @param User $user
     * @param Quiz $quiz
     * @return bool
     */
    public function update(User $user, Quiz $quiz)
    {
        return $user->id === $quiz->user_id;
    }

    /**
     * @param User $user
     * @param Quiz $quiz
     */
    public function close(User $user, Quiz $quiz)
    {
        if ($quiz->status == QuizRepository::QUIZ_STATUS_CLOSED) {
            $this->deny('quiz has been closed');
        }
        return $this->update($user, $quiz);
    }
}
